
# 原始数据
data =[
  10.8008,
  8.5,
  10.5977,
  10.6016,
  10.8984,
  "-99",
  "-99",
  "-99",
  7.1992,
  9.7031,
  10.7969,
  9.9023,
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  10.8008,
  9.3984,
  6.3008,
  2.5,
  5.5,
  "-99",
  "-99",
  "-99",
  -7.6992,
  6,
  3.3984,
  9.6016,
  8.3984,
  9.9023,
  10,
  10.0977,
  "-99",
  "-99",
  -5.5,
  "-99",
  "-99",
  1.1992,
  5.6016,
  9.5,
  "-99",
  "-99",
  9.9023,
  4.3008,
  7.2969,
  8.6016,
  "-99",
  "-99",
  "-99",
  "-99",
  8.5,
  9.8008,
  7.6992,
  8.1016,
  9.5977,
  10.1016,
  10.5,
  4.6016,
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  10.1016,
  8.1992,
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  1.8984,
  "-99",
  "-99",
  "-99",
  2.7969,
  6.6016,
  "-99",
  "-99",
  8.5977,
  2.3008,
  2.3008,
  "-99",
  "-99",
  5.8008,
  5.4023,
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99",
  "-99"
]


import numpy as np
from pyecharts.charts import Line
from pyecharts import options as opts
from scipy import stats



# 将字符串"-99"视为缺失值，并全部转换为float型
data = [np.nan if value == "-99" else float(value) for value in data]

# 创建数组索引
indices = np.arange(len(data))

# 获取有效（非nan）的值和对应的索引
valid_data = np.array([data[i] for i in range(len(data)) if not np.isnan(data[i])])
valid_indices = np.array([indices[i] for i in range(len(data)) if not np.isnan(data[i])])

# 使用线性插值
interpolated_data = np.interp(indices, valid_indices, valid_data)

# 计算大于或等于0.1的数据的众数
mode_value = stats.mode(interpolated_data[interpolated_data >= 0.1])[0][0]

# 把小于0.1的数改为众数
interpolated_data[interpolated_data < 0.1] = mode_value

# 保留三位小数
interpolated_data = np.around(interpolated_data, decimals=3)

# 把最后一段-99的数据恢复回去
for i in range(len(data)-1, -1, -1):
    if np.isnan(data[i]):
        interpolated_data[i] = data[i]
    else:
        break

# 可视化结果
line = (
    Line()
    .add_xaxis(xaxis_data=list(range(len(interpolated_data))))
    .add_yaxis(
        "Interpolated Data",
        y_axis=interpolated_data.tolist(),
        is_smooth=True,
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Data Interpolation Using Linear Interpolation"),
        xaxis_opts=opts.AxisOpts(type_="category"),
        tooltip_opts=opts.TooltipOpts(is_show=False),
    )
)

line.render("linear_interpolation.html")
